home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-07-03 | 14.3 KB | 403 lines | [TEXT/MPS ] |
- (*------------------------------------------------------------------------------
- #
- # Macintosh Developer Technical Support
- #
- # Simple Color QuickDraw Animation Sample Application
- #
- # TubeTest
- #
- # TubeTest.p - Pascal Source
- #
- # Copyright © 1988 Apple Computer, Inc.
- # All rights reserved.
- #
- # Versions: 1.0 8/88
- # 1.01 9/16/92 GAB
- #
- # Components: TubeTest.p August 1, 1988
- # TubeTest.r August 1, 1988
- # TubeTest.make August 1, 1988
- #
- # The TubeTest program is a simple demonstration of how to use the Palette
- # Manager in a color program. It has a special color palette that is associated
- # with the main window. The colors are animated using the Palette Manager
- # to give a flowing tube effect. The program is very simple, and the Palette
- # Manager and drawing parts are put in separate subroutines to make it easier
- # to figure out what is happening.
- #
- # The program is still a complete Macintosh application with a Main Event Loop,
- # so there is the extra code to run the MEL.
- #
- # There is a resource file that is necessary as well, to define the Menus, Window,
- # Dialog, and Palette resources used in the program.
- #
- # See Sample and TESample for the general structure and MultiFinder techniques that
- # we recommend that you use when building a new application.
- #
- ------------------------------------------------------------------------------*)
-
- MODULE TubeTest;
- (*
- File TubeTest.p
-
- Version 1.0: 6/2/88
-
- 4/19/88:
- TubeTest -- A small sample application written by Bo3b Johnson.
- The idea is to draw two circles in varying colors in the window, then
- animate the colors when the menu is chosen. This is a complete program
- with event loop and everything. It is intended to be a simple example of
- how to use the Palette Manager to do some minor color animation, and
- how to use the PM to set up the colors desired in a window.
-
- Also see the resource file that goes with this to see how the Palette
- itself is layed out.
-
- Could be built with something like this:
- rez TubeTest.r -o TubeTest
- pascal TubeTest.p
- Link TubeTest.p.o ∂
- "(*MPW*)Libraries:"Interface.o ∂
- "(*MPW*)Libraries:"Runtime.o ∂
- "(*MPW*)PLibraries:"Paslib.o ∂
- -o TubeTest
- TubeTest
- *)
-
- (*Where does it fit:
- This is a series of sample programs for those doing development
- using Color QuickDraw. Since the whole color problem depends
- upon the exact effect desired, there are a number of answers
- to how to use colors, from the simple to the radically complex.
- These programs try to cover the gamut, so you should use
- which ever seems appropriate. In most cases, use the simplest
- one that will give the desired results. The compatibility
- rating is from 0..9 where low is better. The more known risks
- there are the higher the rating.
-
-
- The programs (in order of compatibility):
-
- SillyBalls:
- This is the simplest use of Color QuickDraw, and does
- not use the Palette Manager. It draws randomly colored
- balls in a color window. This is intended to give you
- the absolute minimum required to get color on the screen.
- Written in straight Pascal code.
- Compatibility rating = 0, no known risks.
-
- FracAppPalette:
- This is a version of FracApp that uses only the Palette
- Manager. It does not support color table animation
- since that part of the Palette Manager is not sufficient.
- The program demonstrates a full color palette that is
- used to display the Mandelbrot set. It uses an offscreen
- gDevice w/ Port to handle the data, using CopyBits to
- draw to the window. The Palette is automatically
- associated with each window. The PICT files are read
- and written using the bottlenecks, to save on memory
- useage.
- Written in MacApp Object Pascal code.
- Compatibility rating = 0, no known risks.
-
- TubeTest: (***)
- This is a small demo program that demonstrates using the
- Palette Manager for color table animation. It uses a
- color palette with animating entries, and draws using the
- Palette Manager. There are two circles of animating colors
- which gives a flowing tube effect. This is a valid case
- for using the animating colors aspect of the Palette Manager,
- since the image is being drawn directly.
- Written in straight Pascal code.
- Compatibility rating = 0, no known risks.
-
- FracApp:
- This is the ‘commercial quality’ version of FracApp. This
- version supports color table animation, using an offscreen
- gDevice w/ Port, and handles multiple documents. The
- CopyBits updates to the screen are as fast as possible. The
- program does not use the Palette Manager, except to
- provide for the system palette, or color modes with less than
- 255 colors. For color table animation using an offscreen
- gDevice w/ Port, it uses the Color Manager and handles the
- colors itself. Strict compatibility was relaxed to allow for
- a higher performance program. This is the most ‘real’ of the
- sample programs.
- Written in MacApp Object Pascal code.
- Compatibility rating = 2. (nothing will break, but it may not
- always look correct.)
-
- FracApp300:
- This doesn't support colors, but demonstrates how to create and
- use a 300 dpi bitmap w/ Port. The bitmap is printed at full
- resolution on LaserWriters, and clipped on other printers (but
- they still print). It demonstrates how to use a high resolution
- image as a PICT file, and how to print them out.
- Written in MacApp Object Pascal code.
- Compatibility rating = 1. (The use of PrGeneral is slightly
- out of the ordinary, although supported.)
- *)
-
- (*$R-*) (* No range checking.*)
- (* $ D+*) (* Debugging labels on. *)
-
-
- (* Interface files with all the happy Macintosh stuff in them. *)
-
- IMPORT SYSTEM, Types, Quickdraw, Memory, Windows, Dialogs, Menus,
- Fonts, TextEdit, Events, OSUtils, Palettes, Desk, SegLoad;
-
-
- CONST
- appleID = 1000; (* resource IDs/menu IDs for Apple, File and Edit menus *)
- fileID = 1001;
- editID = 1002;
-
- appleM = 0; (* Index for each menu in myMenus (array of menu handles) *)
- fileM = 1;
- editM = 2;
-
- menuCount = 3; (* Total number of menus *)
-
- windowID = 1000; (* Resource ID for main window *)
- aboutMeDLOG = 1000; (* And Resource ID for About box dialog. *)
-
- tubularItem = 1; (* When checked, animation of colors. *)
- quitItem = 3; (* Quit in the menu of course. *)
-
- aboutMeCommand = 1; (* Menu item in apple menu for About TubeTest item *)
-
- totalColors = 152; (* use 150 colors in our palette for drawing eyes. *)
- numColors = 150; (* to skip black and white. *)
-
-
-
- VAR
- myMenus: ARRAY menuCount OF Menus.MenuHandle;
- dragRect: Types.Rect; (* Rectangle used to mark bounds for dragging window *)
- doneFlag: BOOLEAN; (* TRUE if user has chosen Quit command *)
- myEvent: Events.EventRecord;
- myWindow: Windows.WindowPtr;
- whichWindow: Windows.WindowPtr;
- tubeCheck: BOOLEAN; (* if true, the menu is checked, and we animate. *)
- theChar: CHAR;
- error: Types.OSErr;
- theWorld: OSUtils.SysEnvRec;
-
- (* This routine will update the window when required by update events. It
- will draw two circular dudes that are indexed in colors through the colors
- we are using. 0 and 1 are skipped, since those are white and black in the
- palette. *)
- PROCEDURE DrawEyes;
-
- VAR TempRect: Types.Rect;
- I: INTEGER;
-
- BEGIN
- Quickdraw.SetRect(TempRect, numColors, numColors, numColors, numColors);
- FOR I := 2 TO totalColors DO
- Palettes.PmForeColor(I);
- Quickdraw.FrameOval (TempRect);
- Quickdraw.InsetRect (TempRect, -1, -1);
- END;
-
- Quickdraw.SetRect(TempRect, numColors*3, numColors, numColors*3, numColors);
- FOR I := totalColors TO 2 BY -1 DO
- Palettes.PmForeColor(I);
- Quickdraw.FrameOval (TempRect);
- Quickdraw.InsetRect (TempRect, -1, -1);
- END;
- END DrawEyes;
-
-
- PROCEDURE SetUpMenus;
-
- VAR I: INTEGER;
-
- BEGIN
- (* Read menu descriptions from resource file into memory and store handles
- in myMenus array *)
- myMenus[appleM] := Menus.GetMenu(appleID); (*read Apple menu from resource file*)
- Menus.AddResMenu(myMenus[appleM], LONG("DRVR")); (*add desk accessory names to Apple menu*)
- myMenus[fileM] := Menus.GetMenu(fileID); (*read file menu from resource file*)
- myMenus[editM] := Menus.GetMenu(editID); (*read edit menu from resource file*)
-
- FOR I := 1 TO menuCount DO Menus.InsertMenu(myMenus[I-1], 0) END; (*install menus in menu bar*)
-
- Menus.DrawMenuBar; (*and draw menu bar*)
- END SetUpMenus;
-
-
- (* Use the Palette currently attached to the main window to animate the colors
- in the circular eye shapes. This will rotate them around to give the flowing
- tube effect. We make the palette into a color table so we can move entries
- around. We have to skip the first two entries since those are black and white.
- (entries 0 and 1) *)
- PROCEDURE ShiftyColors;
-
- VAR currPalette: Palettes.PaletteHandle;
- destCTab: Quickdraw.CTabHandle;
- lastCSpec: Quickdraw.ColorSpec;
-
- (*$MAIN*)
- BEGIN
- Quickdraw.SetPort (myWindow);
-
- currPalette := Palettes.GetPalette(myWindow);
- destCTab := Quickdraw.CTabHandle (Memory.NewHandle (SIZE (Quickdraw.ColorTable)+(totalColors*SIZE(Quickdraw.ColorSpec))));
- IF destCTab = NIL THEN RETURN END;
- Palettes.Palette2CTab(currPalette, destCTab);
-
- (* Move the colors around in the color table, skipping 0 and 1, and moving
- all the elements down by one, and copying the element at 2 back to the
- end of the table. The effect is to rotate the colors in the table. *)
- (*$R- range checking off*)
- lastCSpec := destCTab.ctTable[2]; (* pull first one off. *)
- SYSTEM.MOVE(SYSTEM.ADR(destCTab.ctTable[3]), SYSTEM.ADR(destCTab.ctTable[2]),
- (numColors) * SIZE(Quickdraw.ColorSpec) ); (* copy all one entry down. *)
- destCTab.ctTable[totalColors-1] := lastCSpec; (* put last color back on front. *)
- (*$R+ range checking on*)
-
- Palettes.AnimatePalette(myWindow, destCTab, 2, 2, numColors);
-
- Memory.DisposHandle (Types.Handle (destCTab));
- END ShiftyColors;
-
-
- (* Display the dialog box in response to the 'About TubeTest' menu item. *)
- PROCEDURE ShowAboutMeDialog;
-
- VAR theDialog: Dialogs.DialogPtr;
- itemHit: INTEGER;
-
- BEGIN
- theDialog := Dialogs.GetNewDialog(aboutMeDLOG, NIL, Windows.WindowPtr( - 1));
- Dialogs.ModalDialog(NIL, itemHit);
- Dialogs.DisposDialog(theDialog);
- END ShowAboutMeDialog;
-
-
- (* Execute menu command specified by mResult, the result of MenuSelect *)
- PROCEDURE DoCommand(mResult: LONGINT);
-
- VAR theItem: INTEGER; (* menu item number from mResult low-order word *)
- theMenu: INTEGER; (* menu number from mResult high-order word *)
- name: Types.Str255; (* desk accessory name *)
- temp: INTEGER;
- dummy: BOOLEAN;
-
- BEGIN
- theItem := LoWrd(mResult); (* call Toolbox Utility routines to *)
- theMenu := HiWrd(mResult); (* set menu item number and menu *)
-
- CASE theMenu OF (* case on menu ID *)
-
- appleID:
- IF (theItem = aboutMeCommand) THEN ShowAboutMeDialog
- ELSE (* call Menu Manager to get desk acc.*)
- Menus.GetItem(myMenus[appleM], theItem, name);
- temp := Desk.OpenDeskAcc(name);
- Quickdraw.SetPort(myWindow);
- END| (* appleID *)
-
- fileID:
- IF theItem = quitItem THEN doneFlag := TRUE END;
-
- IF theItem = tubularItem THEN
- tubeCheck := ¬tubeCheck;
- Menus.CheckItem(myMenus[fileM], tubularItem, tubeCheck);
- END| (* fileID *)
-
- editID:
- dummy := Desk.SystemEdit(theItem - 1)|(* Pass the command on to the Desk Manager.*)
-
- ELSE (* ignore other menus *)
- END; (*of menu CASE*)
-
- Menus.HiliteMenu(0); (* Unhighlight menu title(highlighted by MenuSelect) *)
- END DoCommand;
-
-
- BEGIN (* Main *)
- (* Test the computer to be sure we can do color. If not we would crash, which
- would be bad. If we can’t run, just beep and exit. *)
- error := OSUtils.SysEnvirons(1, theWorld);
- IF ¬theWorld.hasColorQD THEN
- OSUtils.SysBeep (50);
- SegLoad.ExitToShell; (* If no color QD, we must leave. *)
- END;
-
- Quickdraw.InitGraf(Quickdraw.thePort);
- Fonts.InitFonts;
- Windows.InitWindows;
- Menus.InitMenus;
- TextEdit.TEInit;
- Dialogs.InitDialogs(NIL);
- Quickdraw.InitCursor;
-
- Quickdraw.SetRect(dragRect, 4, 24, Quickdraw.screenBits.bounds.botRight.h - 4, Quickdraw.screenBits.bounds.botRight.v - 4);
- doneFlag := FALSE; (* flag to detect when Quit command is chosen*)
- tubeCheck := FALSE; (* flag for animating color is initially off. *)
-
- (* Open the color window. *)
- myWindow := Windows.GetNewCWindow(windowID, NIL, Windows.WindowPtr(-1));
- Quickdraw.SetPort(myWindow);
-
- (* Set up menus last, since the menu drawing can then use the palette we have for ou
- window. Makes the Apple look better, in particular. *)
- SetUpMenus;
-
- (* Main Event Loop *)
- REPEAT
- Desk.SystemTask;
-
- IF Events.WaitNextEvent(Events.everyEvent, myEvent, 5, NIL) THEN
- CASE myEvent.what OF (* case on event type*)
-
- Events.mouseDown:
- CASE Windows.FindWindow(myEvent.where, whichWindow) OF
-
- Windows.inSysWindow: (* desk accessory window: call Desk Manager to handle it*)
- Desk.SystemClick(myEvent, whichWindow)|
-
- Windows.inMenuBar: (* Menu bar: learn which command, then execute it. *)
- DoCommand(Menus.MenuSelect(myEvent.where))|
-
- Windows.inDrag: (* title bar: call Window Manager to drag*)
- Windows.DragWindow(whichWindow, myEvent.where, dragRect)|
-
- Windows.inContent: (* body of application window: *)
- IF whichWindow # Windows.FrontWindow() THEN
- Windows.SelectWindow(whichWindow); (*and make it active if not*)
- END
- ELSE (* ignore other events *)
- END| (*of mouseDown*)
-
- Events.updateEvt: (* Update the eyes in window. *)
- IF Windows.WindowPtr(myEvent.message) = myWindow THEN
- Windows.BeginUpdate(Windows.WindowPtr(myEvent.message));
- DrawEyes;
- Windows.EndUpdate(Windows.WindowPtr(myEvent.message));
- END| (*of updateEvt*)
-
- Events.keyDown, Events.autoKey: (* key pressed once or held down to repeat *)
- IF myWindow = Windows.FrontWindow() THEN
- theChar := CHR(BAND(myEvent.message, Events.charCodeMask)); (* get the char *)
- (* If Command key down, do it as a Menu Command. *)
- IF BAND(myEvent.modifiers, Events.cmdKey) # 0 THEN DoCommand(Menus.MenuKey(theChar)) END;
- END; (*of keyDown and autoKey*)
-
- ELSE (* ignore other events *)
- END; (*of event CASE*)
- END;
-
- (* If we have menu item checked, go ahead and animate colors. *)
- IF tubeCheck THEN ShiftyColors END;
-
- UNTIL doneFlag;
-
- (* clean up after palette manager, so he can chuck the palette in use. *)
- Windows.DisposeWindow (myWindow);
-
- END TubeTest.
-